package com.xjprojectc.base.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.xjprojectc.business.pojo.T_agentaccountinfo;
import com.xjprojectc.uitl.DateUtil;

public class DataDaoImpl {
	private static JdbcTemplate jt;

	public JdbcTemplate getJt() {
		return jt;
	}

	@SuppressWarnings("static-access")
	public void setJt(JdbcTemplate jt) {
		this.jt = jt;
	}
	
	/**
	 * 修改短信猫余额
	 * @param id
	 * @param money 本次操作金额
	 * @param afterMoney  余额
	 * @param type (add=新增，decrease=减少，outher=其他)
	 */
	public static synchronized void updateSmsserverMoney(int id,String money,String afterMoney,Double systempromptedmoney,String type) {
		StringBuffer sql = new StringBuffer();
		sql.append("update t_smsserver set balance = ");
		sql.append(afterMoney);
		if(systempromptedmoney != null){
			sql.append(" ,systempromptedmoney=");
			sql.append(systempromptedmoney);
		}
		if(Double.parseDouble(afterMoney)<100){
			sql.append(" ,portmodel=");
			sql.append("'1'");
		}
		sql.append("  where id = ");
		sql.append(id);
		jt.update(sql.toString());
		if(type.equals("add")){
			//updateFinancestatistics(Double.parseDouble(money),2,id);
		}
	}

	/**
	 * 
	 * @param type
	 *            add=加 reduce=减
	 * @param money
	 * @param username
	 * @param id
	 *            缴费列表ID 从充值列表更新余额的时候用
	 */
	@SuppressWarnings("unchecked")
	public static synchronized Double updateAgentBalance( String type,
			 Double money, String username,  String callId) {
		Double afterMoney = 0.0 ;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(new Date());
		String sql = "select id,username,accountmoney,field2,field3 from T_AgentAccountInfo where username='"
				+ username + "'";
		List rows = jt.queryForList(sql);
		Iterator it = rows.iterator();
		while(it.hasNext()){
			Map result=(Map)it.next();
			T_agentaccountinfo info = new T_agentaccountinfo();
			info.setId(Integer.parseInt(result.get("id").toString()));
			info.setUsername(result.get("username").toString());
			info.setAccountmoney(Double.parseDouble(result.get("accountmoney").toString()));
			info.setField2(result.get("field2").toString());
			info.setField3(result.get("field3").toString());
			int timeDifference = DateUtil.timeDifference(time, info.getField2(), "s");
			if (timeDifference < 11) {
				try {
					Thread.sleep(10000);
					List rows_s = jt.queryForList(sql);
					Iterator it_s = rows_s.iterator();
					while(it_s.hasNext()){
						info = new T_agentaccountinfo();
						Map result_s = (Map)it.next();
						info.setId(Integer.parseInt(result_s.get("id").toString()));
						info.setUsername(result_s.get("username").toString());
						info.setAccountmoney(Double.parseDouble(result_s.get("accountmoney").toString()));
						info.setField2(result_s.get("field2").toString());
						info.setField3(result_s.get("field3").toString());
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}else {
				info.setField2(time);
			}
			
			
			if (type.equals("add")) {
				afterMoney = info.getAccountmoney() + money;
			} else {
				afterMoney = info.getAccountmoney()-money;
				info.setAccountmoney(afterMoney);
				if(callId != null && !"".equals(callId)){
					info.setField3(callId);
				}
			}
			
			String updateSql = "update T_AgentAccountInfo set accountmoney = "
					+ afterMoney
					+ ",field2='"
					+ info.getField2()
					+ "',field3='"
					+ info.getField3()
					+ "' where id = "
					+ info.getId();
			jt.update(updateSql);
			break;
		}
		return afterMoney;
	}
	
//	
//	/**
//	 * 
//	 * 代理商财务日志充值更新
//	 * @param money
//	 * @param agentId
//	 * @return
//	 */
//	@SuppressWarnings("unchecked")
//	private static  boolean updateFinancestatistics(Double money,int type ,int agentId){
//		boolean bool = false;
//		String sql = "select id, paymentmoney from t_financestatistics where type= "+type+" and remark='"+agentId+"' and day = '"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"'";
//		List rows = jt.queryForList(sql);
//		Iterator it = rows.iterator();
//		while(it.hasNext()){
//			Map result =(Map)it.next();
//			Object paymentmoney = result.get("paymentmoney");
//			if(paymentmoney != null && !"".equals(paymentmoney)){
//				money += Double.parseDouble(paymentmoney.toString());
//			}
//			jt.update("update t_financestatistics set paymentmoney="+money+" where id = "+result.get("id"));
//		}	
//		return bool;
//	}
}
